<?php
	
	//前台购物车控制器
	class ShopcartAction extends Action{
		
		/*   
		 * 显示购物车页面
		 */
		
		
		public function cart(){
			//判断用户是否登录
			if(!session('?m_username')){
				$this->redirect('Register/login',array(),2,'您还没有登录，正在进入登录系统···');
			}
			if($this->isPost()){
				//接收数据，把数据入库（购物车的数据ku）
				$g_id=$_POST['goods_id'];
				$m_id=$_POST['user_id'];
				$g_inv=$_POST['goods_number']+0;
				$goods=D('Goods');
				$row=$goods->field('g_name,g_price,g_sn,g_thumb_img')->find($g_id);
				$g_allprice=$g_inv * $row['g_price'];
				$_POST['shop_price']=$row['g_price'];
				$_POST['goods_name']=$row['g_name'];
				$_POST['goods_sn']=$row['g_sn'];
				$_POST['session_id']=session_id();
				$_POST['goods_thumb']=$row['g_thumb_img'];
				$cart=D('Cart');
				if($cart->create()){
					
					$id=$cart->add();
					if($id){
						
						$this->redirect("shopcart");
					}else{
						$this->error('加入购物车失败',"__GROUP__/Index/deta/id/$g_id");
					}
					
				}else{
					$this->error($cart->getError());
				}
			}
		}
		//加入购物车
		public function shopcart(){
			$m_id=session('m_id');
			//实例化购物车信息模型
			$cart=D('Cart');
			//查询出当前用户加入购物车的商品
			$allgoods=$cart->where("user_id='$m_id'")->order('id desc')->select();
			$this->assign('allgoods',$allgoods);
			
			foreach($allgoods as $v){
				static $allprice=0;
				$allprice+=($v['goods_number']*$v['shop_price']);
			}
	
			//实例化商品信息模型
			$goods=D('Goods');
			//查询所有的新品和热销商品	
			$data=$goods->where("g_is_new=1")->order('g_id desc')->select();
			$this->assign('data',$data);
			$data1=$goods->where("g_is_hot=1")->order('g_id desc')->select();
			$this->assign('data1',$data1);
			$this->assign('allprice',$allprice);
	
	    $this->display();	
		}
		
		//删除购物车的单个商品
		public function del(){
			//接受当前要删除的商品id
			$id=$_GET['id']+0;
			$cart=D('Cart');
			//查询出当前用户加入购物车的商品
			if($cart->delete($id)){
				$this->success('删除商品成功','__URL__/shopcart');
			}else{
				$this->error('删除商品失败',"__URL__/shopcart");
			}
			
		}
		//清空购物车商品
		public function delAll(){
			//接受当前要删除的商品id
			$user_id=$_GET['u_id']+0;
			$cart=D('Cart');
			
			$sql="delete from hfp_cart where user_id='$user_id'";
			//查询出当前用户加入购物车的商品
			if($cart->execute($sql)){
				$this->success('清空购物车商品成功','__URL__/shopcart');
			}else{
				$this->error('清空购物车商品失败',"__URL__/shopcart");
			}
				
		}
		
		
		//用于加入到购物车的一个函数：
		public function additem($goods_id,$session_id,$goods_attr,$user_id,$goods_name,$shop_price,$goods_number,$goods_attr_price,$goods_attr_id){
			//在商品加入购物车之前要判断一下该商品购物车里面是否存在，如果存在则数量加1，如果不存在则添加。
			if($this->hasitem($goods_id,$session_id,$goods_attr)){
				//存在
				$sql="update cart set goods_number=goods_number+$goods_number where goods_id=$goods_id and session_id='$session_id' and goods_attr='$goods_attr'";
			}else{
				//不存在ss
				$sql="insert into cart (goods_id,user_id,session_id,goods_name,shop_price,goods_number,goods_attr,goods_attr_price,goods_attr_id) values($goods_id,$user_id,'$session_id','$goods_name',$shop_price,$goods_number,'$goods_attr','$goods_attr_price','$goods_attr_id')";
			}
			//执行sql语句
			$cartmodel = D('Cart');
			if($cartmodel->Execute($sql)!==false){
				return true;
			}
			return false;
		
		}
		//判断购物车里面有没有同一件商品
		public function hasitem($goods_id,$session_id,$goods_attr){
			$cart = D("Cart");
			$cartdata = $cart->where("goods_id=$goods_id and session_id='$session_id'")->select();
			if($cartdata){
				return true;
			}else{
				return false;
			}
		}
	}